package com.zbkj.common.utils;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * OssUtil使用示例类
 * 演示如何使用OssUtil进行文件下载
 */
public class OssUtilExample {
    
    /**
     * 示例1：下载文件为字节数组
     */
    public static void downloadAsBytesExample() {
        try {
            // 从OSS对象名称下载
            String objectName = "client/1/123/material/example.jpg";
            byte[] fileBytes = OssUtil.downloadFileAsBytes(objectName);
            System.out.println("下载文件大小: " + fileBytes.length + " 字节");
            
            // 从完整URL下载
            String fullUrl = "https://vision-smart.oss-cn-shanghai.aliyuncs.com/client/1/123/material/example.jpg";
            byte[] fileBytesFromUrl = OssUtil.downloadFileFromUrl(fullUrl);
            System.out.println("从URL下载文件大小: " + fileBytesFromUrl.length + " 字节");
            
        } catch (Exception e) {
            System.err.println("下载失败: " + e.getMessage());
        }
    }
    
    /**
     * 示例2：使用输入流下载大文件
     */
    public static void downloadAsStreamExample() {
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        
        try {
            String objectName = "client/1/123/material/large-file.pdf";
            inputStream = OssUtil.downloadFileAsStream(objectName);
            
            // 保存到本地文件
            fileOutputStream = new FileOutputStream("downloaded-file.pdf");
            byte[] buffer = new byte[1024];
            int bytesRead;
            
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
            
            System.out.println("文件下载完成");
            
        } catch (Exception e) {
            System.err.println("下载失败: " + e.getMessage());
        } finally {
            // 关闭资源
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                System.err.println("关闭资源失败: " + e.getMessage());
            }
        }
    }
    
    /**
     * 示例3：从URL提取对象名称
     */
    public static void extractObjectNameExample() {
        try {
            String fullUrl = "https://vision-smart.oss-cn-shanghai.aliyuncs.com/client/1/123/material/example.jpg";
            String objectName = OssUtil.extractObjectNameFromUrl(fullUrl);
            System.out.println("提取的对象名称: " + objectName);
            // 输出: client/1/123/material/example.jpg
            
        } catch (Exception e) {
            System.err.println("提取对象名称失败: " + e.getMessage());
        }
    }
    
    /**
     * 示例4：在Web应用中流式下载
     */
    public static void streamDownloadExample(javax.servlet.http.HttpServletResponse response) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        
        try {
            String objectName = "client/1/123/material/document.pdf";
            inputStream = OssUtil.downloadFileAsStream(objectName);
            
            // 设置响应头
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment; filename=document.pdf");
            
            // 流式传输
            outputStream = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            
            outputStream.flush();
            
        } catch (Exception e) {
            System.err.println("流式下载失败: " + e.getMessage());
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                System.err.println("关闭资源失败: " + e.getMessage());
            }
        }
    }
    
    /**
     * 示例5：使用FileDownloadUtil进行安全的文件下载
     */
    public static void safeDownloadExample(javax.servlet.http.HttpServletResponse response) {
        try {
            String objectName = "client/1/123/material/document.pdf";
            InputStream inputStream = OssUtil.downloadFileAsStream(objectName);
            
            // 使用FileDownloadUtil进行安全的文件下载
            String fileName = "document.pdf";
            String contentType = FileDownloadUtil.getContentType(fileName);
            FileDownloadUtil.downloadFile(inputStream, fileName, contentType, response);
            
        } catch (Exception e) {
            System.err.println("安全下载失败: " + e.getMessage());
        }
    }
    
    /**
     * 示例6：测试文件名处理
     */
    public static void testFileNameProcessing() {
        System.out.println("=== 测试文件名处理 ===");
        FileDownloadUtil.testFileNameProcessing();
    }
    
    /**
     * 主方法，运行所有示例
     */
    public static void main(String[] args) {
        System.out.println("=== OssUtil使用示例 ===");
        
        System.out.println("\n1. 下载文件为字节数组:");
        downloadAsBytesExample();
        
        System.out.println("\n2. 使用输入流下载大文件:");
        downloadAsStreamExample();
        
        System.out.println("\n3. 从URL提取对象名称:");
        extractObjectNameExample();
        
        System.out.println("\n4. 测试文件名处理:");
        testFileNameProcessing();
        
        System.out.println("\n示例运行完成！");
    }
} 